{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#初识pytorch\n",
    "import torch\n",
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpu 0.5167407989501953 tensor(1.00000e+05 *\n",
      "       1.4151)\n"
     ]
    }
   ],
   "source": [
    "a = torch.randn(100000,1000)\n",
    "b = torch.randn(1000,200)\n",
    "t1 = time.time()\n",
    "c = torch.matmul(a,b)\n",
    "t2 = time.time()\n",
    "print(a.device, t2 - t1,c.norm(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 2,  3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 2.3000,  3.0000,  3.0000], dtype=torch.float64)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#numpy\n",
    "q = np.array([2.3,3,3])\n",
    "p = np.ones([2,3])\n",
    "torch.from_numpy(p)\n",
    "torch.from_numpy(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1,  2,  3],\n",
       "        [ 2,  3,  4]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#list tensor只有一个参数\n",
    "torch.tensor([1,2])\n",
    "torch.tensor([[1,2,3],[2,3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0.0000e+00, -2.6844e+08,  0.0000e+00, -2.6844e+08],\n",
       "         [ 2.0667e+09,  3.2707e+04,  2.0668e+09,  3.2707e+04],\n",
       "         [ 2.0668e+09,  3.2707e+04,  0.0000e+00,  3.0000e+00]],\n",
       "\n",
       "        [[ 0.0000e+00,  0.0000e+00,  2.0279e+09,  3.2707e+04],\n",
       "         [ 0.0000e+00, -2.6844e+08,  0.0000e+00, -2.6844e+08],\n",
       "         [ 3.0000e+00,  0.0000e+00,  0.0000e+00,  1.0000e+00]]], dtype=torch.int32)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#uninitialized\n",
    "torch.empty(2)\n",
    "torch.Tensor([1,2])#Tensor有多个参数时表示size\n",
    "torch.IntTensor(2,3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 6.,  9.,  3.],\n",
       "        [ 2.,  7.,  5.],\n",
       "        [ 6.,  9.,  7.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#rand\n",
    "a = torch.rand(2,3)\n",
    "torch.rand_like(a)\n",
    "torch.randint(1,10,[3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.5418, -0.7873, -0.3838, -0.8312, -0.8709, -1.1153, -0.5277,\n",
       "        -0.2725,  0.0039,  0.0492])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#randn normal\n",
    "torch.randn(2,3)\n",
    "torch.normal(torch.full([10],0),torch.arange(1,0,-0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 7.,  7.,  7.],\n",
       "        [ 7.,  7.,  7.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#full\n",
    "torch.full([10],7)\n",
    "torch.full([2,3],7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.,  3.,  5.,  7.,  9.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#arange range包含end arange不包含end\n",
    "torch.arange(1,10)\n",
    "torch.arange(1,10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,\n",
       "         10.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.range(0,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "torch.set_default_tensor_type(torch.DoubleTensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.0000e+00,  2.1544e+03,  4.6416e+06,  1.0000e+10])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#linspace logspace\n",
    "torch.linspace(0,10,steps=4)\n",
    "#tensor([  0.0000,   3.3333,   6.6667,  10.0000])\n",
    "torch.logspace(0,10,steps=4)#10的多少次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  0.,  0.,  0.],\n",
       "        [ 0.,  1.,  0.,  0.],\n",
       "        [ 0.,  0.,  1.,  0.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#ones zeros\n",
    "torch.ones([3,3])\n",
    "torch.zeros([3,3])\n",
    "torch.eye(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 3,  5,  1,  6,  8,  9,  4,  7,  0,  2])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#randperm\n",
    "torch.randperm(10)#range之后再shuffle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1, 28, 28])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#索引和切片\n",
    "a = torch.Tensor(4,3,28,28)\n",
    "a[...,:26].shape\n",
    "a[:,2:,:,:].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 10, 28])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#index_select\n",
    "b = torch.Tensor(4,3,28,28)\n",
    "b.index_select(0,torch.tensor([0,2])).shape\n",
    "b.index_select(2,torch.arange(10).long()).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.2380, -2.4434,  0.2223])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#select by mask 铺平之后为1的index对应的地方取值\n",
    "c = torch.randn(4,3,28,28)\n",
    "mask = c.ge(0.5)\n",
    "torch.masked_select(c,mask).shape\n",
    "torch.take(c,torch.tensor([2,5,8]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.5968,  0.3332,  0.4461,  ...,  0.0480,  0.7832,  0.2061],\n",
       "        [ 0.8994,  0.3544,  0.6772,  ...,  0.6867,  0.5363,  0.0773],\n",
       "        [ 0.7549,  0.3362,  0.7810,  ...,  0.5065,  0.9984,  0.1730],\n",
       "        [ 0.3782,  0.9534,  0.0392,  ...,  0.0243,  0.0321,  0.3834]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#rand是生成均匀分布,而randn是生成均值为0,方差为1的正态分布\n",
    "d = torch.rand([4,1,28,28])\n",
    "d.view(4,28*28)#会丢失size信息\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 28, 28])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#squeeze 和 unsqueeze\n",
    "e = torch.rand([4,1,28,28])\n",
    "torch.unsqueeze(e,1).shape#类似于tf.expand\n",
    "torch.squeeze(e,1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
